//
//  ST_BTPairSuccessController.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/9.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import NIMSDK

class ST_BTPairSuccessController: UIViewController {

    
    var someone:ST_MediaInfoModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = Color.init(hexString: "#FD7078")
        
        view.addSubview(pair_title_label)
        view.addSubview(pairContentLabel)
        
        let offsetY = iPhoneXSeries ? 115 + statusBarHeight : 115
        
        pair_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(offsetY)
            make.centerX.equalToSuperview()
        }
        
        pairContentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(pair_title_label.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        
        
        
        view.addSubview(chat_btn)
        view.addSubview(cancel_btn)
        
        
        chat_btn.snp.makeConstraints { (make) in
            make.width.equalTo(260)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
        }
        
        cancel_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(chat_btn)
            make.centerX.equalToSuperview()
            make.top.equalTo(chat_btn.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-44-BTSafeAreaBottom)
        }
        
        view.addSubview(heart_image_view)
        heart_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(pairContentLabel.snp.bottom).offset(104)
        }
        
        view.addSubview(left_image_view)
        left_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(100)
            make.top.equalTo(heart_image_view).offset(-40)
            make.right.equalTo(heart_image_view.snp.left).offset(-21)
        }
        
        let moveX = 150.cgFloat
        
        left_image_view.transform = CGAffineTransform.init(translationX: -moveX, y: 0)
        
        
        view.addSubview(right_image_view)
        right_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(100)
            make.top.equalTo(heart_image_view).offset(-40)
            make.left.equalTo(heart_image_view.snp.right).offset(21)
        }
        
        right_image_view.transform = CGAffineTransform.init(translationX: moveX, y: 0)
        
        main_thread_execute(afterDelay: 0.3) {
            
            UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 7, options:  UIView.AnimationOptions.allowUserInteraction, animations: {
                    
                self.left_image_view.transform = CGAffineTransform.identity
                self.right_image_view.transform = CGAffineTransform.identity

            }) { (f) in
                
            }
        }
        
        main_thread_execute(afterDelay: 0.5) {
            
            UIView.animate(withDuration: 0.5) {
                
                self.heart_image_view.alpha = 1
            }
        }
        
        
    
        guard let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo else { return
        }
        
        
        let nameStr = someone.video_member_nickname
        
        
        
        let str = String.init(format: "你和%@一定很多想聊的呢！", nameStr)
        
        pairContentLabel.text = str
        
        
        left_image_view.pin_setImage(from: user.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        right_image_view.pin_setImage(from: someone.video_member_avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        
    }
    
    
    
    

//    MARK: - Properties
    
   
    
    fileprivate lazy var pair_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "恭喜成为好友！"
        control.textColor = Color.white
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 24, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var pairContentLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.white
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var chat_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.white
        control.cornerRadius = 40/2.0
        control.setTitleForAllStates("立即聊天")
        control.setTitleColor(UIColor.init(hexString: "#FD7078")!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(chat_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var cancel_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.clear
        control.setTitleForAllStates("再继续看看")
        control.setTitleColor(UIColor.white, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

    fileprivate lazy var heart_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("首页_icon_配对成功")
        control.alpha = 0
        return control
    }()
   
    fileprivate lazy var left_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 100/2.0
        control.borderWidth = 2
        control.borderColor = Color.white
        return control
    }()

    
    fileprivate lazy var right_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 100/2.0
        control.borderWidth = 2
        control.borderColor = Color.white
        return control
    }()

    
    
//    MARK: - Event
    var dismissClosure:(()->())?
    
    var chatClosure:((ST_MediaInfoModel)->())?
    
    @objc func chat_handle(){
        
        self.chatClosure?(someone)
    }
    
    @objc func cancel_handle(){
        
        self.dismissClosure?()
        
        self.goback_previous_controller()
    }
    
    
}
